import requests
import openpyxl

cookies = {
    'll': '"108296"',
    'bid': '0meeVryP3wE',
    'ap_v': '0,6.0',
    '_qimei_uuid42': '19b16130d12100afcee0a942ea4f26532801f1d7d1',
    '_qimei_fingerprint': 'a229050914a38b1064b6f8f37aeeb75e',
    '_qimei_i_3': '7bb37dd6930903dc90c6aa615cd175e6ffeca0f91a520480bd8d2b5f7495246f326661943989e2a0b393',
    '__utma': '30149280.2130737740.1763809999.1763809999.1763809999.1',
    '__utmc': '30149280',
    '__utmz': '30149280.1763809999.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)',
    '__utmt_t1': '1',
    '__utmb': '30149280.3.8.1763809999',
    '_qimei_h38': '4656ad09cee0a942ea4f26530200000eb19b16',
    '_qimei_i_1': '23c37d86935f568a9597f7630ed173b1feeaa6a4435f5580b28a2b582493206c616362c73980b0ddd0b6e3c2',
    'RT': '',
    '_vwo_uuid_v2': 'D96D0A7AB11BC12FDCC53F8EEB93EF0FD|6c8bf06edb37fd692173c231b7f0fafd',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'origin': 'https://movie.douban.com',
    'priority': 'u=1, i',
    'referer': 'https://movie.douban.com/',
    'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    # 'cookie': 'll="108296"; bid=0meeVryP3wE; ap_v=0,6.0; _qimei_uuid42=19b16130d12100afcee0a942ea4f26532801f1d7d1; _qimei_fingerprint=a229050914a38b1064b6f8f37aeeb75e; _qimei_i_3=7bb37dd6930903dc90c6aa615cd175e6ffeca0f91a520480bd8d2b5f7495246f326661943989e2a0b393; __utma=30149280.2130737740.1763809999.1763809999.1763809999.1; __utmc=30149280; __utmz=30149280.1763809999.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt_t1=1; __utmb=30149280.3.8.1763809999; _qimei_h38=4656ad09cee0a942ea4f26530200000eb19b16; _qimei_i_1=23c37d86935f568a9597f7630ed173b1feeaa6a4435f5580b28a2b582493206c616362c73980b0ddd0b6e3c2; RT=; _vwo_uuid_v2=D96D0A7AB11BC12FDCC53F8EEB93EF0FD|6c8bf06edb37fd692173c231b7f0fafd',
}

params = {
    'limit': '50',
    'category': '热门',
    'type': '全部',
}

response = requests.get(
    'https://m.douban.com/rexxar/api/v2/subject/recent_hot/movie',
    params=params,
    cookies=cookies,
    headers=headers,
)
#print(response.json())
data = response.json().get("items")
for item in data:
    title = item.get("title")
    card_subtitle = item.get("card_subtitle")
    count = item.get("rating").get("count")
    value= item.get("rating").get("value")
    large_pic = item.get("pic").get("large")
    print(title,card_subtitle,count,value,large_pic)

#存储到Excel表格
#创建新工作簿
workbook = openpyxl.Workbook()
#获取默认工作表
sheet = workbook.active
sheet.append(["电影名称","主演","评分","评价人数","图片链接"])
for item in data:
    title = item.get("title")
    card_subtitle = item.get("card_subtitle")
    count = item.get("rating").get("count")
    value = item.get("rating").get("value")
    large_pic = item.get("pic").get("large")
    sheet.append([title,card_subtitle,value,count,large_pic])
workbook.save("电影.xlsx")